package reservation.flight;

import com.crittercism.app.Crittercism;

import java.io.InputStream;
import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import reservation.flight.*;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
	InputStream is =null;
	 ArrayList<NameValuePair> nameValuePairs=null;
    /** Called when the activity is first created. */
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Crittercism.init(getApplicationContext(), "4fa28d2ab0931540ba0000a4");
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.main);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.title_bar);
		
        final EditText login=(EditText)findViewById(R.id.Main_User_ET);
        final EditText pass=(EditText)findViewById(R.id.Main_Pass_ET);
        final TextView texterror=(TextView)findViewById(R.id.Main_Error_TW);
        Button b_login=(Button)findViewById(R.id.Main_Login_B);
        Button b_registrar=(Button)findViewById(R.id.Main_New_User_B);
       
       b_login.setOnClickListener(new OnClickListener() {
    	   
			public void onClick(View v) {
    		   texterror.setText("");
    		   String s_login, s_pass;
    		   s_login=login.getText().toString();
    		   s_pass=pass.getText().toString();
    		   if(s_login.length()>0&&s_pass.length()>0){
    			    nameValuePairs = new ArrayList<NameValuePair>();
	   		        nameValuePairs.add(new BasicNameValuePair("login",s_login));
	   		        nameValuePairs.add(new BasicNameValuePair("pass",s_pass));
	   		        nameValuePairs.add(new BasicNameValuePair("opcion","login"));
	   		        Function fun=new Function();
	   		        String result= fun.conexion(nameValuePairs);
	   		        int res= fun.loginUsuario(result);
	   		        if(res==1){
		   		        nameValuePairs.add(new BasicNameValuePair("opcion","iduser"));
		   		        fun=new Function();
		   		        result= fun.conexion(nameValuePairs);
		   		        res= fun.develverIdUsuario(result);
	   		        	Log.e("log_tag","Entra al Menu Principal "+res);
	   		        	Bundle bundle = new Bundle();
	   			        bundle.putString("ID", res+"");  
	   		        	Intent i = new Intent(getApplicationContext(),
	   	                        MainMenuActivity.class);
	   		        	i.putExtras(bundle);
	   	                startActivity(i);
	   		        }else
	   		        	texterror.setText(getString(R.string.user_or_pass));
    		   }else{
    			   texterror.setText(getString(R.string.empty_field));
    		   }
				
				
			}
       });	     
       b_registrar.setOnClickListener(new OnClickListener() {
		
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i = new Intent(getApplicationContext(),
                       NewUserActivity.class);
               startActivity(i);
		}
	});
        
    }
}